$(function() {

  var thislocalUrl = window.location.href;
  var locationurl = location.href.split("?")[0];
  var search = $("#inquery_list_form");
  var searchurlq = search[0].action;
  /*if(thislocalUrl == locationurl){
    getListParams('int','');
  }else{
    getParamt(0);
  }*/
  
  //滚动条拉到底部的时候去获取新的数据
  var stop=true; 
  var pageNumb = 1;

  scrollUrl();

  $(document).on("click",".loadMore",function(e){
    if(stop==true){ 
        stop=false;  
        scrollUrl();
    } 
    stopDefault(e);
  })

  //滚动调用的方法;
  function scrollUrl(){
    var location_searc = window.location.search;
    var scrollUrl = searchurlq+location_searc;
    var url;
    if(location_searc == ""){
      url = scrollUrl+'?page='+pageNumb++;
      getAllUrl(url)
      stop=true;
    }else{
      getParamt(0);
      url = scrollUrl+'&page='+pageNumb++;
      getAllUrl(url)
      stop=true;
    }
  }
  
 //根据搜索条件查询
 $(document).on("click","#subSearch",function(e){
    var keyword = $("#keyword").val();
    if(thislocalUrl == locationurl){
      var params ='q='+keyword;
      getListParams('',params);

    }else{
      getParamt(1);
    }
    stopDefault(e);
  })

 //根据条件查询
  $(".area a,.issue a,.trade a,.order a").click( function (e) {
    var qthis = $(this);
    getClickVal(qthis);
    getParamt(2);
    stopDefault(e);
  });

//获取页面返回的参数，并且把有值的变为选中状态
select_val();
function select_val(){
    var value_area = getUrlParam("city_id"); 
    var value_issue = getUrlParam("published"); 
    var value_trade = getUrlParam("cat_id"); 
    var value_order = getUrlParam("order"); 
    var value_q = getUrlParam("q");

    var area = $(".area a");
    var issue = $(".issue a");
    var trade = $(".trade a");
    var order = $(".order a");

    if( value_area != null){
      for(var i=0;i<area.length;i++){
        var val_area = area[i].attributes[1].value;
        if(val_area == value_area){
          area[i].className = "active";
          break;
        }
      }
    }else{
      $(".area a").eq(0).addClass("active")
    }
    if( value_issue != null){
      for(var i=0;i<issue.length;i++){
        var val_issue = issue[i].attributes[1].value;
        if(val_issue == value_issue){
          issue[i].className = "active";
          break;
        }
      }
    }else{
      $(".issue a").eq(0).addClass("active")
    }

    if( value_trade != null){
      for(var i=0;i<trade.length;i++){
        var val_trade = trade[i].attributes[1].value;
        if(val_trade == value_trade){
          trade[i].className = "active";
          break;
        }
      }
    }else{
      $(".trade a").eq(0).addClass("active")
    }

    if( value_order != null){
      for(var i=0;i<order.length;i++){
        var val_order = order[i].attributes[1].value;
        if(val_order == value_order){
          order[i].className = "active";
          break;
        }
      }
    }else{
      $(".order a").eq(0).addClass("active")
    }
    if( value_q != null){
      $("keyword").val(value_q);
    }else{
      $("keyword").val("");
    }
    
}

    /* 公用js */
  $(".btn-more").toggle(
      function () {
        $(this).parent().find(".workscope").addClass("unfolded")
      },
      function () {
        $(this).parent().find(".workscope").removeClass("unfolded");
      }
  );

});

//获取参数，处理被收藏书签的情况
function getParamt(index){
    var locat_area = getUrlParam("city_id"); 
    var locat_issue = getUrlParam("published"); 
    var locat_trade = getUrlParam("cat_id"); 
    var locat_order = getUrlParam("order"); 
    var locat_q = getUrlParam("q");
    var keyword = $("#keyword").val();
    
    var params = [];
    if (locat_area) {
      params.push('city_id=' + locat_area);
    }
    if (locat_issue) {
      params.push('published=' + locat_issue);
    }
    if (locat_trade) {
      params.push('cat_id=' + locat_trade);
    }
    if (locat_order) {
      params.push('order=' + locat_order);
    }

    if(index == 0){ //index=0初始化
      if(locat_q == null && keyword != ""){
         params.push('q=' + keyword);
      }else if(locat_q == null && keyword == ""){
        return false;
      }else if(locat_q != null && keyword == ""){
        params.push('q=' + locat_q);
        $("#keyword").val(locat_q);
      }else{
        
      }

    }

   if(index == 1){ //输入搜索条件查找
      if (locat_q) {
        if(locat_q != keyword){
          params.push('q=' + keyword);
        }else{
          params.push('q=' + locat_q);
        }
      }else{
        params.push('q=' + keyword);
      }
    }

  if(index == 2){ //点击a标签根据条件查找
      var area = $(".area a.active").attr("data-id");
      var issue = $(".issue a.active").attr("data-id");
      var trade = $(".trade a.active").attr("data-id");
      var order = $(".order a.active").attr("data-id");
      var keyword = $("#keyword").val();
      var value_q = getUrlParam("q");
      var params = [];
      if (area) {
        params.push('city_id=' + area);
      }
      if (issue) {
        params.push('published=' + issue);
      }
      if (trade) {
        params.push('cat_id=' + trade);
      }
      if (order) {
        params.push('order=' + order);
      }

      if(value_q != null){
        if(value_q != keyword){
          params.push('q=' + keyword);
        }else{
          params.push('q=' + value_q);
          $("keyword").val(value_q);
        }
          
      }else{
        var q = $("keyword").val();
        if(q != null){
           params.push('q=' + q);
        }
        
      }

    }
    //console.info(params)
    params = params.join('&');
    getListParams('',params);

}


//点击选中添加背景颜色
function getClickVal(qthis){
   var $this = qthis;
    var parenta = $this.parent();
    var worksa = parenta.find(".workscope").children("a");
    var any = parenta.parent().children("a");
    //不同级
    any.removeClass("active");
    worksa.removeClass("active");
    //同级情况下
    $this.addClass("active").siblings().removeClass("active");
    //获取到选中的值
    var thisVal = $this.attr("data-id");
}

//获取数据调用的显示的列表
function getListParams(init,params){
   //共用获取数据的请求路径
   var search = $("#inquery_list_form");
   var searchurlq = search[0].action;
   var url;
    if(init != ""){
      url = searchurlq;//初始化
      //url = searchurlq+'?rows_per_page=1000';
      $("#page").empty(); //删除tbody中已经存在的数据
      getAllUrl(url);
    }
    if(params != ""){
      url = searchurlq+'?'+params; //带参数
      var thisUrl = location.href.split("?")[0]+'?'+params;
      window.history.pushState('obj', document.title, thisUrl);
      $("#page").empty(); //删除tbody中已经存在的数据
      getAllUrl(url);
    }else{
      url = searchurlq; //带参数
      var thisUrl = location.href.split("?")[0];
      window.history.pushState('obj', document.title, thisUrl);
      $("#page").empty(); //删除tbody中已经存在的数据
      getAllUrl(url);
    }
}

//调用不同的url,追加不一样的内容
function getAllUrl(url){ 
      var loading = '<dl><div class="loading">加载中...</div></dl>'; 
      $("#page").append(loading);
      $(".loading").eq(1).parent().remove();
      // console.info(url);
       $.get(url, function(data){
              
          }, "json").error(function(data) {
              var status_code = $.parseJSON(data.responseText);
              layer.msg(status_code.message)
          }).success(function(data) {
           $(".loading").parent().remove();
            var str  =  "";
            var obj = data.data;
            if(obj.length > 0){

              for(var i=0; i<obj.length; i++)  
              {  
                obj[i].views_count == undefined ? obj[i].views_count =0 : obj[i].views_count;
                obj[i].likes_count == undefined ? obj[i].likes_count =0 : obj[i].likes_count;
                obj[i].comments_count == undefined ? obj[i].comments_count =0 : obj[i].comments_count;

                 //返回数据位json格式，遍历json，拼凑成table中的行
                  str +='<dl class="issueClassInfo clear">'
                    str +='<dt><b>问题分类</b><label class="dateTime">'+obj[i].created_at+'</label></dt>'
                    str +='<dd><b><a href="'+jumpUrl+'/question/'+obj[i].id+'">'+obj[i].title+'</a></b></dd>'
                    str +='<dd class="issuetxt"><a href="'+jumpUrl+'/question/'+obj[i].id+'">'+obj[i].content+'</a></dd>'
                    str +='<dd>'
                        str +=''
                        str +=' <div class="numlr">'
                          str +='<a href="javascript:void(0);" title="阅读"><label class="readNumber">'+obj[i].views_count+'</label></a><b class="line">|</b>'
                          str +='<a href="javascript:void(0);" title="点赞"><label class="like">'+obj[i].likes_count+'</label></a><b class="line">|</b>'
                          str +='<a href="javascript:void(0);" title="评论"><label class="review last">'+obj[i].comments_count+'</label></a>'
                        str +='</div>'
                    str +='</dd>'
                  str +='</dl>';
              }  
              
            }else{
              str+='<dl class="nodata">';
                  str+='暂无数据！';
              str+='</dl>';
            }

            //追加如tbody中 
            $("#page").append(str); 
            $(".nodata").eq(1).remove()
      });
}
